package com.elite.groovy.callback

import cn.com.tpUtil.AES
import com.elite.groovy.util.EliteBean
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

public class InnerApiGetAESUserCode implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(InnerApiGetAESUserCode.class);
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

		response.setCharacterEncoding("UTF-8");
		log.debug("start GetDataInBound-----------------");
		log.debug("request--:"+request);

		String getMethod = request.getMethod();
		String callJson="";
		BufferedReader iny = null;

		def reJson = "";

		log.debug("getMethod:"+getMethod);
		if (getMethod.equals("GET")) {
			callJson = request.getParameter("data");
			log.debug("原始Get请求参数:"+callJson);
		} else {
			InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
			iny = new BufferedReader(reader);
			String line;
			while ((line = iny.readLine()) != null) {
				callJson += line;
			}
			log.debug("原始Post请求参数:"+callJson);
		}


		try {
			//获取配置的userCode
			//String userCode = PropertiesUtil.getProp("userCode");
			String userCode = request.getParameter("userCode");
			log.debug("配置userCode:"+userCode);


			//并AES加密
			String AESuserCode  = AES.encrypt(userCode);

			//输出AES结果
			log.debug("AES加密返回串:"+AESuserCode);

			//成功返回
			reJson= "{\"code\":\"1\",\"message\":\""+AESuserCode+"\"}";
			log.debug("getAESUserCode返回Json:"+reJson);
			return reJson;
		}
		catch(Exception e) {
			log.debug("接口处理异常"+e.getMessage());
			//失败返回
			reJson= "{\"code\":\"0\",\"description\":\"错误:"+e.getMessage()+"\"}";
			log.debug("getAESUserCode返回Json:"+reJson);
			return reJson;
		}
	}
}
